package org.web.standardplatform.client.domain.buildproject;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.web.standardplatform.client.helper.BuilderHelper;

@Setter
@Getter
public class ProjectDefinitionDO {

    // reference maven groupId
    private String groupId;
    // reference maven artifactId
    private String artifactId;
    // reference maven version
    private String version;
    // the directory of project.
    private String outputPath;
    // the directory of templatePath;
    private String templatePath;

    private ParentProjectDefinitionDO parentProjectDefinitionDO;

    private ServerProjectDefinitionDO serverProjectDefinitionDO;

    private ClientProjectDefinitionDO clientProjectDefinitionDO;

    private ClientDemoProjectDefinitionDO clientDemoProjectDefinitionDO;

    private StarterProjectDefinitionDO starterProjectDefinitionDO;

    public void init() {
        if (StringUtils.isEmpty(getVersion())) {
            setVersion("1.0.0");
        }

        this.initParent(this);
        this.initClient(this);
        this.initServer(this);
        this.initDemo(this);
        this.initStarter(this);
    }


    public void initStarter(ProjectDefinitionDO projectDefinitionDO) {
        if (projectDefinitionDO.getStarterProjectDefinitionDO() == null) {
            projectDefinitionDO.setStarterProjectDefinitionDO(new StarterProjectDefinitionDO());
        }
        if (StringUtils.isEmpty(projectDefinitionDO.getStarterProjectDefinitionDO().getStarterProjectName())) {
            projectDefinitionDO.getStarterProjectDefinitionDO().setStarterProjectName(projectDefinitionDO.getArtifactId() + "-starter");
        }
        StarterProjectDefinitionDO starterProjectDefinitionDO = projectDefinitionDO.getStarterProjectDefinitionDO();
        starterProjectDefinitionDO.setStarterSpringBootName(BuilderHelper.transferLine2Humps(serverProjectDefinitionDO.getServerProjectName(), ProjectDefinitionConstant.HALF_WAY_LINE, true) + "SpringBootStarter");
    }


    public void initParent(ProjectDefinitionDO projectDefinitionDO) {
        if (projectDefinitionDO.getParentProjectDefinitionDO() == null) {
            projectDefinitionDO.setParentProjectDefinitionDO(new ParentProjectDefinitionDO());
        }
        if (StringUtils.isEmpty(projectDefinitionDO.getParentProjectDefinitionDO().getParentProjectName())) {
            projectDefinitionDO.getParentProjectDefinitionDO().setParentProjectName(projectDefinitionDO.getArtifactId() + "-parent");
        }
    }

    public void initClient(ProjectDefinitionDO projectDefinitionDO) {
        if (ObjectUtils.isEmpty(projectDefinitionDO.getClientProjectDefinitionDO())) {
            projectDefinitionDO.setClientProjectDefinitionDO(new ClientProjectDefinitionDO());
        }
        ClientProjectDefinitionDO clientProjectDefinitionDO = projectDefinitionDO.getClientProjectDefinitionDO();

        if (StringUtils.isEmpty(clientProjectDefinitionDO.getSuffix())) {
            clientProjectDefinitionDO.setSuffix("-sdk");
        }

        if (StringUtils.isEmpty(clientProjectDefinitionDO.getClientProjectName())) {
            if (StringUtils.isNotBlank(projectDefinitionDO.getClientProjectDefinitionDO().getSuffix())) {
                clientProjectDefinitionDO.setClientProjectName(projectDefinitionDO.getArtifactId() + clientProjectDefinitionDO.getSuffix());
            } else {
                clientProjectDefinitionDO.setClientProjectName(projectDefinitionDO.getArtifactId() + "-sdk");
            }
        }
    }

    public void initDemo(ProjectDefinitionDO projectDefinitionDO) {
        if (ObjectUtils.isEmpty(projectDefinitionDO.getClientDemoProjectDefinitionDO())) {
            projectDefinitionDO.setClientDemoProjectDefinitionDO(new ClientDemoProjectDefinitionDO());
        }
        ClientDemoProjectDefinitionDO clientDemoProjectDefinitionDO = projectDefinitionDO.getClientDemoProjectDefinitionDO();

        if (StringUtils.isEmpty(clientDemoProjectDefinitionDO.getSuffix())) {
            clientDemoProjectDefinitionDO.setSuffix("-demo");
        }

        if (StringUtils.isEmpty(clientDemoProjectDefinitionDO.getClientDemoProjectName())) {
            if (StringUtils.isNotBlank(projectDefinitionDO.getClientDemoProjectDefinitionDO().getSuffix())) {
                clientDemoProjectDefinitionDO.setClientDemoProjectName(projectDefinitionDO.getArtifactId() + clientDemoProjectDefinitionDO.getSuffix());
            } else {
                clientDemoProjectDefinitionDO.setClientDemoProjectName(projectDefinitionDO.getArtifactId() + "-demo");
            }
        }

        clientDemoProjectDefinitionDO.setDemoProjectClassName(BuilderHelper.transferLine2Humps(clientDemoProjectDefinitionDO.getClientDemoProjectName(), ProjectDefinitionConstant.HALF_WAY_LINE, true));
        clientDemoProjectDefinitionDO.setDemoProjectStartClassName(BuilderHelper.transferLine2Humps(clientDemoProjectDefinitionDO.getClientDemoProjectName(), ProjectDefinitionConstant.HALF_WAY_LINE, true) + "Starter");
        clientDemoProjectDefinitionDO.setDemoProjectStartClassPackagePathName(projectDefinitionDO.getGroupId() + "." + BuilderHelper.transferLine2Humps(projectDefinitionDO.getArtifactId(), ProjectDefinitionConstant.HALF_WAY_LINE, false).toLowerCase() + "." + clientDemoProjectDefinitionDO.getSuffix().replaceAll("-", "").toLowerCase());
    }

    public void initServer(ProjectDefinitionDO projectDefinitionDO) {

        if (ObjectUtils.isEmpty(projectDefinitionDO.getServerProjectDefinitionDO())) {
            projectDefinitionDO.setServerProjectDefinitionDO(new ServerProjectDefinitionDO());
        }
        ServerProjectDefinitionDO serverProjectDefinitionDO = projectDefinitionDO.getServerProjectDefinitionDO();
        if (StringUtils.isEmpty(serverProjectDefinitionDO.getSuffix())) {
            serverProjectDefinitionDO.setSuffix("-server");
        }
        if (StringUtils.isEmpty(serverProjectDefinitionDO.getServerProjectName())) {
            serverProjectDefinitionDO.setServerProjectName(projectDefinitionDO.getArtifactId() + serverProjectDefinitionDO.getSuffix());
        }
        serverProjectDefinitionDO.setServerProjectClassName(BuilderHelper.transferLine2Humps(serverProjectDefinitionDO.getServerProjectName(), ProjectDefinitionConstant.HALF_WAY_LINE, true));
        serverProjectDefinitionDO.setServerProjectStartClassName(BuilderHelper.transferLine2Humps(serverProjectDefinitionDO.getServerProjectName(), ProjectDefinitionConstant.HALF_WAY_LINE, true) + "Starter");
        serverProjectDefinitionDO.setServerProjectStartClassPackagePathName(projectDefinitionDO.getGroupId()+"."+projectDefinitionDO.getArtifactId().toLowerCase().replaceAll("-","")+"."+projectDefinitionDO.getServerProjectDefinitionDO().getSuffix().replaceAll("-","").toLowerCase());
        serverProjectDefinitionDO.setDapPackagePathName(serverProjectDefinitionDO.getServerProjectStartClassPackagePathName() + ".dao");
    }

    public String getBasePackage() {
        return getGroupId() + "." + BuilderHelper.transferLine2Humps(getArtifactId(), ProjectDefinitionConstant.HALF_WAY_LINE, false).toLowerCase();
    }
}
